<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源" prop="origin">
        <el-input
          v-model="queryParams.origin"
          placeholder="请输入来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="备注" prop="note">
        <el-input
          v-model="queryParams.note"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="后台订单id" prop="backorderId">
        <el-input
          v-model="queryParams.backorderId"
          placeholder="请输入后台订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组别 Group" prop="groupId">
        <el-input
          v-model="queryParams.groupId"
          placeholder="请输入组别 Group"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Priority" prop="priority">
        <el-input
          v-model="queryParams.priority"
          placeholder="请输入Priority"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划日期">
        <el-date-picker
          v-model="daterangeScheduledDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="daterangeDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="完成日期">
        <el-date-picker
          v-model="daterangeDateDone"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="位置" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Destination Location" prop="locationDestId">
        <el-input
          v-model="queryParams.locationDestId"
          placeholder="请输入Destination Location"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拣货类型id" prop="pickingTypeId">
        <el-input
          v-model="queryParams.pickingTypeId"
          placeholder="请输入拣货类型id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合作伙伴id" prop="partnerId">
        <el-input
          v-model="queryParams.partnerId"
          placeholder="请输入合作伙伴id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司id" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入公司id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="Owner" prop="ownerId">
        <el-input
          v-model="queryParams.ownerId"
          placeholder="请输入Owner"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打印" prop="printed">
        <el-input
          v-model="queryParams.printed"
          placeholder="请输入打印"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否锁定" prop="isLocked">
        <el-input
          v-model="queryParams.isLocked"
          placeholder="请输入是否锁定"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新日期" prop="messageLastPost">
        <el-date-picker clearable
          v-model="queryParams.messageLastPost"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="截至时间" prop="activityDateDeadline">
        <el-date-picker clearable
          v-model="queryParams.activityDateDeadline"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择截至时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建者" prop="createUid">
        <el-select v-model="queryParams.createUid" placeholder="请选择创建者" clearable>
          <el-option
            v-for="dict in dict.type.boshien_emp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建日期" prop="createDate">
        <el-date-picker clearable
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新者" prop="writeUid">
        <el-select v-model="queryParams.writeUid" placeholder="请选择更新者" clearable>
          <el-option
            v-for="dict in dict.type.boshien_emp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="更新日期" prop="writeDate">
        <el-date-picker clearable
          v-model="queryParams.writeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="销售员" prop="saleId">
        <el-select v-model="queryParams.saleId" placeholder="请选择销售员" clearable>
          <el-option
            v-for="dict in dict.type.boshien_emp"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:picking:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:picking:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:picking:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:picking:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pickingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="来源" align="center" prop="origin" />
      <el-table-column label="备注" align="center" prop="note" />
      <!-- <el-table-column label="后台订单id" align="center" prop="backorderId" /> -->
      <!-- <el-table-column label="移动类型" align="center" prop="moveType" /> -->
      <!-- <el-table-column label="状态" align="center" prop="state" /> -->
      <!-- <el-table-column label="组别 Group" align="center" prop="groupId" /> -->
      <!-- <el-table-column label="Priority" align="center" prop="priority" /> -->
      <el-table-column label="计划日期" align="center" prop="scheduledDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scheduledDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成日期" align="center" prop="dateDone" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateDone, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="位置" align="center" prop="locationId" /> -->
      <!-- <el-table-column label="Destination Location" align="center" prop="locationDestId" /> -->
      <!-- <el-table-column label="拣货类型id" align="center" prop="pickingTypeId" /> -->
      <!-- <el-table-column label="合作伙伴id" align="center" prop="partnerId" /> -->
      <!-- <el-table-column label="公司id" align="center" prop="companyId" /> -->
      <!-- <el-table-column label="Owner" align="center" prop="ownerId" /> -->
      <!-- <el-table-column label="打印" align="center" prop="printed" /> -->
      <!-- <el-table-column label="是否锁定" align="center" prop="isLocked" /> -->
      <!-- <el-table-column label="更新日期" align="center" prop="messageLastPost" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.messageLastPost, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="截至时间" align="center" prop="activityDateDeadline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityDateDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="创建者" align="center" prop="createUid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.boshien_emp" :value="scope.row.createUid"/>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="writeUid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.boshien_emp" :value="scope.row.writeUid"/>
        </template>
      </el-table-column>
      <el-table-column label="更新日期" align="center" prop="writeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.writeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="销售员" align="center" prop="saleId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.boshien_emp" :value="scope.row.saleId"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="getDetail(scope.row)"
           >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
           
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:picking:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存拣货对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="来源" prop="origin">
          <el-input v-model="form.origin" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="后台订单id" prop="backorderId">
          <el-input v-model="form.backorderId" placeholder="请输入后台订单id" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="组别 Group" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入组别 Group" />
        </el-form-item>
        <el-form-item label="Priority" prop="priority">
          <el-input v-model="form.priority" placeholder="请输入Priority" />
        </el-form-item>
        <el-form-item label="计划日期" prop="scheduledDate">
          <el-date-picker clearable
            v-model="form.scheduledDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成日期" prop="dateDone">
          <el-date-picker clearable
            v-model="form.dateDone"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择完成日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="位置" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="Destination Location" prop="locationDestId">
          <el-input v-model="form.locationDestId" placeholder="请输入Destination Location" />
        </el-form-item>
        <el-form-item label="拣货类型id" prop="pickingTypeId">
          <el-input v-model="form.pickingTypeId" placeholder="请输入拣货类型id" />
        </el-form-item>
        <el-form-item label="合作伙伴id" prop="partnerId">
          <el-input v-model="form.partnerId" placeholder="请输入合作伙伴id" />
        </el-form-item>
        <el-form-item label="公司id" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司id" />
        </el-form-item>
        <el-form-item label="Owner" prop="ownerId">
          <el-input v-model="form.ownerId" placeholder="请输入Owner" />
        </el-form-item>
        <el-form-item label="打印" prop="printed">
          <el-input v-model="form.printed" placeholder="请输入打印" />
        </el-form-item>
        <el-form-item label="是否锁定" prop="isLocked">
          <el-input v-model="form.isLocked" placeholder="请输入是否锁定" />
        </el-form-item>
        <el-form-item label="更新日期" prop="messageLastPost">
          <el-date-picker clearable
            v-model="form.messageLastPost"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截至时间" prop="activityDateDeadline">
          <el-date-picker clearable
            v-model="form.activityDateDeadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择截至时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建者" prop="createUid">
          <el-select v-model="form.createUid" placeholder="请选择创建者">
            <el-option
              v-for="dict in dict.type.boshien_emp"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建日期" prop="createDate">
          <el-date-picker clearable
            v-model="form.createDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新者" prop="writeUid">
          <el-select v-model="form.writeUid" placeholder="请选择更新者">
            <el-option
              v-for="dict in dict.type.boshien_emp"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="更新日期" prop="writeDate">
          <el-date-picker clearable
            v-model="form.writeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="销售员" prop="saleId">
          <el-select v-model="form.saleId" placeholder="请选择销售员">
            <el-option
              v-for="dict in dict.type.boshien_emp"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPicking, getPicking, delPicking, addPicking, updatePicking } from "@/api/system/picking";
import pickingdetail from '@/views/system/pickingdetail/index.vue'
export default {
  name: "Picking",
  dicts: ['boshien_emp'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存拣货表格数据
      pickingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 销售员时间范围
      daterangeScheduledDate: [],
      // 销售员时间范围
      daterangeDate: [],
      // 销售员时间范围
      daterangeDateDone: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        origin: null,
        note: null,
        backorderId: null,
        moveType: null,
        state: null,
        groupId: null,
        priority: null,
        scheduledDate: null,
        date: null,
        dateDone: null,
        locationId: null,
        locationDestId: null,
        pickingTypeId: null,
        partnerId: null,
        companyId: null,
        ownerId: null,
        printed: null,
        isLocked: null,
        messageLastPost: null,
        activityDateDeadline: null,
        createUid: null,
        createDate: null,
        writeUid: null,
        writeDate: null,
        saleId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        moveType: [
          { required: true, message: "移动类型不能为空", trigger: "change" }
        ],
        locationId: [
          { required: true, message: "位置不能为空", trigger: "blur" }
        ],
        locationDestId: [
          { required: true, message: "Destination Location不能为空", trigger: "blur" }
        ],
        pickingTypeId: [
          { required: true, message: "拣货类型id不能为空", trigger: "blur" }
        ],
        companyId: [
          { required: true, message: "公司id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
     // 查询详情按钮
     getDetail(row) {
      const id = row.id
      this.$router.push({
        path: "/pickingdetail",
        component: pickingdetail,
        redirect: "noRedirect",
        hidden: false,
        alwaysShow: true,
        query: { listId: id },
      });

    },
    /** 查询库存拣货列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeScheduledDate && '' != this.daterangeScheduledDate) {
        this.queryParams.params["beginScheduledDate"] = this.daterangeScheduledDate[0];
        this.queryParams.params["endScheduledDate"] = this.daterangeScheduledDate[1];
      }
      if (null != this.daterangeDate && '' != this.daterangeDate) {
        this.queryParams.params["beginDate"] = this.daterangeDate[0];
        this.queryParams.params["endDate"] = this.daterangeDate[1];
      }
      if (null != this.daterangeDateDone && '' != this.daterangeDateDone) {
        this.queryParams.params["beginDateDone"] = this.daterangeDateDone[0];
        this.queryParams.params["endDateDone"] = this.daterangeDateDone[1];
      }
      listPicking(this.queryParams).then(response => {
        // console.log(response);
        this.pickingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        origin: null,
        note: null,
        backorderId: null,
        moveType: null,
        state: null,
        groupId: null,
        priority: null,
        scheduledDate: null,
        date: null,
        dateDone: null,
        locationId: null,
        locationDestId: null,
        pickingTypeId: null,
        partnerId: null,
        companyId: null,
        ownerId: null,
        printed: null,
        isLocked: null,
        messageLastPost: null,
        activityDateDeadline: null,
        createUid: null,
        createDate: null,
        writeUid: null,
        writeDate: null,
        saleId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeScheduledDate = [];
      this.daterangeDate = [];
      this.daterangeDateDone = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存拣货";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPicking(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存拣货";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePicking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPicking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库存拣货编号为"' + ids + '"的数据项？').then(function() {
        return delPicking(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/picking/export', {
        ...this.queryParams
      }, `picking_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
